package org.linlinjava.litemall.db.service;

import java.time.LocalDateTime;
import java.util.List;

import javax.annotation.Resource;

import org.beetl.sql.core.query.LambdaQuery;
import org.linlinjava.litemall.db.domain.LitemallIssue;
import org.linlinjava.litemall.db.mapper.LitemallIssueDao;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

@Service
public class LitemallIssueService {
    @Resource
    private LitemallIssueDao issueMapper;

    public void deleteById(Integer id) {
    	
    	LitemallIssue issue = new LitemallIssue();
    	issue.setDeleted(true);
    	issue.setId(id);
    	issueMapper.updateTemplateById(issue);
        //issueMapper.logicalDeleteByPrimaryKey(id);
    }

    public void add(LitemallIssue issue) {
        issue.setAddTime(LocalDateTime.now());
        issue.setUpdateTime(LocalDateTime.now());
        issueMapper.insertTemplate(issue,true);
       // issueMapper.insertSelective(issue);
    }

    public List<LitemallIssue> querySelective(String question, Integer page, Integer limit, String sort, String order) {
    	
    	LambdaQuery<LitemallIssue> query = issueMapper.createLambdaQuery();
    	 if (!StringUtils.isEmpty(question)) {
    		 query.andLike(LitemallIssue::getQuestion, "%" + question + "%");
             //criteria.andQuestionLike("%" + question + "%");
         }
    	 query.andEq(LitemallIssue::getDeleted, false);
    	 if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
    		 query.orderBy(sort + " " + order);
         }
    	 
    	 query.limit(page, limit);
    	 return query.select();
			/*
			 * LitemallIssueExample example = new LitemallIssueExample();
			 * LitemallIssueExample.Criteria criteria = example.createCriteria();
			 * 
			 * if (!StringUtils.isEmpty(question)) { criteria.andQuestionLike("%" + question
			 * + "%"); } criteria.andDeletedEqualTo(false);
			 * 
			 * if (!StringUtils.isEmpty(sort) && !StringUtils.isEmpty(order)) {
			 * example.setOrderByClause(sort + " " + order); }
			 * 
			 * PageHelper.startPage(page, limit); return
			 * issueMapper.selectByExample(example);
			 */
    }

    public int updateById(LitemallIssue issue) {
        issue.setUpdateTime(LocalDateTime.now());
        return issueMapper.updateTemplateById(issue);
        //return issueMapper.updateByPrimaryKeySelective(issue);
    }

    public LitemallIssue findById(Integer id) {
    	return issueMapper.single(id);
        //return issueMapper.selectByPrimaryKey(id);
    }
}
